'use strict'

import cryptoJs from 'crypto-js'

const keyOne = process.env.VUE_APP_KEY || 'error'
// niZKV9PIoq0
export default {
  // 加密函數
  jiami(word) {
    // let key = cryptoJs.enc.Utf8.parse(keyOne);
    const key = cryptoJs.enc.Base64.parse(keyOne)
    let enc = ''
    const option = {
      mode: cryptoJs.mode.ECB,
      padding: cryptoJs.pad.Pkcs7
    }
    if (typeof word === 'string') {
      enc = cryptoJs.DES.encrypt(word, key, option)
    } else if (typeof word === 'object') {
      const data = JSON.stringify(word)
      enc = cryptoJs.DES.encrypt(data, key, option)
    }
    return enc.toString()
  },
  // 解密函數
  jiemi(word) {
    const key = cryptoJs.enc.Base64.parse(keyOne)
    const dec = cryptoJs.DES.decrypt(
      {
        ciphertext: cryptoJs.enc.Base64.parse(word)
      },
      key,
      {
        mode: cryptoJs.mode.ECB,
        padding: cryptoJs.pad.Pkcs7
      }
    )

    return dec.toString(cryptoJs.enc.Utf8)
  }
}
